import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;

public class JDBCTest2 {
    public static void main(String[] args) throws SQLException {
//插入操作
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号：");
        int id = scanner.nextInt();
        System.out.println("请输入姓名:");
        String name = scanner.next();

        //?表示占位符，在后面会被替换掉
        String sql = "insert into student values(?,?)";

        //把这个sql字符换成一个对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        System.out.println(statement);

        //执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret="+ret);

        //释放相关资源
        statement.close();
        connection.close();

    }

}
